#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::on_btn_link_f316_clicked()
{
    if(LangureStu == 1)
    {
        LangureStr = "Connect";
    }
    else
    {
        LangureStr = "连  接";
    }
    if(ui->btn_link_f316->text() == LangureStr)
    {
        //下拉菜单控件失能
        ui->cbx_port_f316->setEnabled(false);
        LangureStr_Init_Btn_Unlink(ui->btn_link_f316);

        LangureStr_Init_Testing(ui->te_prin);
        //设置串口名
        CPortName_f316 = ui->cbx_port_f316->currentText();
        if(!CPortName_f316.isEmpty())
        {
            QString PortName = CPortName_f316.split(":").at(0);
            Port_f316->setPortName(PortName);

            RecvStu_f316 = false;
            ConnStu_f316 = 1;

            //设置波特率
            Port_f316->setBaudRate(115200);
            //设置停止位
            Port_f316->setStopBits(QSerialPort::OneStop);
            //设置数据位数
            Port_f316->setDataBits(QSerialPort::Data8);
            //设置奇偶校验
            Port_f316->setParity(QSerialPort::NoParity);
            //设置流控制
            Port_f316->setFlowControl(QSerialPort::NoFlowControl);

            //打开串口
            if(!Port_f316->open(QIODevice::ReadWrite))
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Unable to open serial port";
                }
                else
                {
                    LangureStr = "无法打开串口";
                }
                InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);

                on_btn_unlink_f316_clicked();
                return;
            }

            PortStu_f316 = PORT_STU_SYNC;
            SyncTimer_f316->start(1000);
        }
        else
        {
            if(ConnStu_f316)
            {
                on_btn_unlink_f316_clicked();
            }
            else
            {
                //
                ui->btn_upg_f316->setEnabled(true);
                ui->btn_read_f316->setEnabled(true);
                ui->btn_write_f316->setEnabled(true);

                ui->cbx_port_f316->setEnabled(true);
                LangureStr_Init_Btn_Link(ui->btn_link_f316);

                // 20230211-1617 新增检测错误提示
                //if(LangureStu == 1)
                //{
                //    LangureStr = "Detection failed, no ports can be connected!";
                //}
                //else
                //{
                //    LangureStr = "检测失败，没有可以连接的端口!";
                //}
                //InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
            }
            return;
        }
    }
    else
    {
        on_btn_unlink_f316_clicked();
    }
}

void MainWindow::on_btn_unlink_f316_clicked()
{
	QByteArray LineStr;
	LineStr.clear();
	Port_f316_Send_Data(LineStr, SEND_PORT_DISC);

    ConnStu_f316 = 0;
	DevVersion = 0;
    if(LangureStu == 1)
    {
        LangureStr = "Current equipment version number: unrecognized";
    }
    else
    {
        LangureStr = "当前设备版本号：未识别";
    }
    ui->lbl_ver_f316->setText(LangureStr);

    if(SyncTimer_f316->isActive())
        SyncTimer_f316->stop();
    if(WaitTimer_f316->isActive())
        WaitTimer_f316->stop();
	//关闭串口
	Port_f316->clear();
    Port_f316->close();

	PortStu_f316 = PORT_STU_NULL;
	LastPortStu_f316 = PORT_STU_NULL;

	//
    Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
    //
    ui->cbx_port_f316->setEnabled(true);
	LangureStr_Init_Btn_Link(ui->btn_link_f316);
}

// 升级
void MainWindow::on_btn_upg_f316_clicked()
{
    SyncTimer_f316->stop();
    Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
    //
    if(LangureStu == 1)
    {
        LangureStr = "The upgrade data is being transferred. Please do not disconnect the device...";
    }
    else
    {
        LangureStr = "正在传输升级数据，请勿断开设备...";
    }
    InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    if(Port_f316_Upgrade_File())
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer_f316->start(TIMER_SYNC_TIME_OUT);
    if(PORT_STU_NULL == PortStu_f316)
    {
        on_btn_unlink_f316_clicked();
    }
}

// 读取
void MainWindow::on_btn_read_f316_clicked()
{
    SyncTimer_f316->stop();
    Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
    //
    if(LangureStu == 1)
    {
        LangureStr = "Reading parameters...";
    }
    else
    {
        LangureStr = "正在读取参数...";
    }
    InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    if(Port_f316_Read_Param())
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer_f316->start(TIMER_SYNC_TIME_OUT);
    if(PORT_STU_NULL == PortStu_f316)
    {
        on_btn_unlink_f316_clicked();
    }
}
// 写入
void MainWindow::on_btn_write_f316_clicked()
{
    SyncTimer_f316->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
    //
    if(LangureStu == 1)
    {
        LangureStr = "Writing parameters...";
    }
    else
    {
        LangureStr = "正在写入参数...";
    }
    InsertText(ui->te_prin_f316, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    if(Port_f316_Write_Param())
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_F316_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer_f316->start(TIMER_SYNC_TIME_OUT);
    if(PORT_STU_NULL == PortStu_f316)
    {
        on_btn_unlink_f316_clicked();
    }
}

// 还原默认
void MainWindow::on_btn_default_f316_clicked()
{
    double BatCurrent = 0.01;
    int BatTime = 1;
    // 充电电流
    ui->ds_cc_f316->setValue(BatCurrent);
    // 充电时间
    ui->sb_cctime_f316->setValue(BatTime);
    // 放电电流
    ui->ds_dc_f316->setValue(BatCurrent);
    // 放电时间
    ui->sb_dctime_f316->setValue(BatTime);
}

void MainWindow::on_cbx_upg_f316_clicked()
{
    bool CbxStu = ui->cbx_upg_f316->isChecked();
    ui->btn_upg_f316->setEnabled(CbxStu);
}

//锁定解锁状态分支
void MainWindow::Port_F316_Lock_Or_Unlock(int EnabledStu)
{
    switch(EnabledStu) {
    case DISP_BTN_LOCK_ALL:{
        ui->btn_read_f316->setEnabled(false);
        ui->btn_write_f316->setEnabled(false);
        ui->btn_default_f316->setEnabled(false);
        ui->btn_upg_f316->setEnabled(false);
        ui->cbx_upg_f316->setEnabled(false);

        ui->cbx_upg_f316->setCheckState(Qt::Unchecked);
        break;
    }
    case DISP_BTN_UNLOCK_ALL:
    case DISP_BTN_LOCK_RES:{
        ui->btn_read_f316->setEnabled(true);
        ui->btn_write_f316->setEnabled(true);
        ui->btn_default_f316->setEnabled(true);
        ui->btn_upg_f316->setEnabled(false);
        ui->cbx_upg_f316->setEnabled(true);

        ui->cbx_upg_f316->setCheckState(Qt::Unchecked);
        break;
    }
    }
}

void MainWindow::on_hs_cc_f316_valueChanged(int value)
{
    if(hs_cc_f316_flag == 0)
    {
        hs_cc_f316_flag = 1;
        int BarMax = ui->hs_cc_f316->maximum();
        double CcMax  = ui->ds_cc_f316->maximum();
        double CcVal  = CcMax * value / BarMax;
        ui->ds_cc_f316->setValue(CcVal);
    }
    else
    {
        hs_cc_f316_flag = 0;
    }
}
void MainWindow::on_ds_cc_f316_valueChanged(double arg1)
{
    if(hs_cc_f316_flag == 0)
    {
        hs_cc_f316_flag = 1;
        int BarMax = ui->hs_cc_f316->maximum();
        double CcMax = ui->ds_cc_f316->maximum();
        double BarVal = arg1 * BarMax / CcMax;
        ui->hs_cc_f316->setValue(BarVal);
    }
    else
    {
        hs_cc_f316_flag = 0;
    }
}

void MainWindow::on_hs_dc_f316_valueChanged(int value)
{
    if(hs_dc_f316_flag == 0)
    {
        hs_dc_f316_flag = 1;
        int BarMax = ui->hs_dc_f316->maximum();
        double DcMax  = ui->ds_dc_f316->maximum();
        double DcVal  = DcMax * value / BarMax;
        ui->ds_dc_f316->setValue(DcVal);
    }
    else
    {
        hs_dc_f316_flag = 0;
    }
}
void MainWindow::on_ds_dc_f316_valueChanged(double arg1)
{
    if(hs_dc_f316_flag == 0)
    {
        hs_dc_f316_flag = 1;
        int BarMax = ui->hs_dc_f316->maximum();
        double DcMax = ui->ds_dc_f316->maximum();
        double BarVal = arg1 * BarMax / DcMax;
        ui->hs_dc_f316->setValue(BarVal);
    }
    else
    {
        hs_dc_f316_flag = 0;
    }
}

void MainWindow::on_hs_cctime_f316_valueChanged(int value)
{
    if(hs_cctime_f316_flag == 0)
    {
        hs_cctime_f316_flag = 1;
        int BarMax = ui->hs_cctime_f316->maximum();
        int CcTimeMax = ui->sb_cctime_f316->maximum();
        int CcTimeVal = CcTimeMax * value / BarMax;
        ui->sb_cctime_f316->setValue(CcTimeVal);
    }
    else
    {
        hs_cctime_f316_flag = 0;
    }
}
void MainWindow::on_sb_cctime_f316_valueChanged(int arg1)
{
    if(hs_cctime_f316_flag == 0)
    {
        hs_cctime_f316_flag = 1;
        int BarMax = ui->hs_cctime_f316->maximum();
        int CcTimeMax = ui->sb_cctime_f316->maximum();
        int BarVal = arg1 * BarMax / CcTimeMax;
        ui->hs_cctime_f316->setValue(BarVal);
    }
    else
    {
        hs_cctime_f316_flag = 0;
    }
}

void MainWindow::on_hs_dctime_f316_valueChanged(int value)
{
    if(hs_dctime_f316_flag == 0)
    {
        hs_dctime_f316_flag = 1;
        int BarMax = ui->hs_dctime_f316->maximum();
        int DcTimeMax = ui->sb_dctime_f316->maximum();
        int DcTimeVal = DcTimeMax * value / BarMax;
        ui->sb_dctime_f316->setValue(DcTimeVal);
    }
    else
    {
        hs_dctime_f316_flag = 0;
    }
}
void MainWindow::on_sb_dctime_f316_valueChanged(int arg1)
{
    if(hs_dctime_f316_flag == 0)
    {
        hs_dctime_f316_flag = 1;
        int BarMax = ui->hs_dctime_f316->maximum();
        int DcTimeMax = ui->sb_dctime_f316->maximum();
        int BarVal = arg1 * BarMax / DcTimeMax;
        ui->hs_dctime_f316->setValue(BarVal);
    }
    else
    {
        hs_dctime_f316_flag = 0;
    }
}
